<div id="main">
    <div id="contents">
        <form method="post" name="filters">
            <fieldset>
                <legend>
                    <label sid="tools.td8"></label>
                </legend>
                <div class="frame_tab_layer">
                    <table class="cmenutitle" width="100%" cellspacing="0" cellpadding="0" border="0">
                        <tbody>
                        <tr>
                            <td class="frame_tab_left">
                                <label sid="fxo_capture.td1"></label>
                            </td>
                            <td class="frame_tab_right">
                                <input class="line-input" type="text" truevalue="" onblur="check();" name="lineid">
                                <a id="button1" name="button" href="#">
                                    <img border="0" src="images/r.gif">
                                </a>
                                <br>
                                <span id="note" style="padding-left:10px;">&nbsp;</span>
                            </td>
                        </tr>
                        <tr>
                            <td align="center" colspan="2">&nbsp;</td>
                        </tr>
                        <tr>
                            <td align="center" colspan="2">
                                <input class="button" type="button" name="start" sid="sbutton.start">
                                <input class="button" type="button" name="stop" sid="sbutton.stops">
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </fieldset>
        </form>
    </div>
</div>
<br>
<div id="helpContainer">
    <div id="help">
        <div>
            <h2>
                <label sid="share.help"></label>
            </h2>
        </div>
        <div id="helpBody">
        <dl>
            <dt class="term">
                <label sid="fxo_capture.exp1"></label>
            </dt>
            <dd class="definition">
                <span style="margin-left:25px;"></span>
                <label sid="fxo_capture.exp2"></label>
            </dd>
            <dt class="term">
                <label sid="fxo_capture.exp3"></label>
            </dt>
            <dd class="definition">
                <b><label sid="fxo_capture.step1"></label></b>
                <span style="margin-left:16px;"></span>
                <label sid="fxo_capture.exp4_1"></label><img border="0" src="images/r.gif"><label sid="fxo_capture.exp4_2"></label><br><br>
                <b><label sid="fxo_capture.step2"></label></b>
                <span style="margin-left:16px;"></span>
                <label sid="fxo_capture.exp5"></label><br><br>
                <b><label sid="fxo_capture.step3"></label></b>
                <span style="margin-left:16px;"></span>
                <label sid="fxo_capture.exp6"></label><br><br>
                <b><label sid="fxo_capture.step4"></label></b>
                <span style="margin-left:16px;"></span>
                <label sid="fxo_capture.exp7"></label>
            </dd>
        </dl>
        </div>
    </div>
</div>
<div id="layer">
    <table id="linetest" class="cmenutitle" width="100%" cellspacing="0" cellpadding="0" border="0">
        <tbody>
        <tr>
            <td>
                <img src="images/load.gif">
            </td>
        </tr>
        </tbody>
    </table>
</div>

<script type="text/javascript">


    var cannotModify;
    var noline;
    var name;
    var num;
    cannotModify = msg.cannotModify1;
    var success = msg.startSuccess;
    var noComp = msg.noComp;
    var already =msg.alreadyCap;
    var input = msg.noLineInput;
    var timeout = msg.timeout;
    var nofile = msg.nodownload;
    var dlfail = msg.downloadFail;
    var push = msg.pushStart;
    var lineNumberError = msg.lineNumberError;
    noline = msg.noLine;
    name = msg.lineName;
    num = msg.lineNumber;
    var td1 = fxo_capture.td1;
    var td2 = fxo_capture.td2;
    var ensure = sbutton.ok;
    var cancel = sbutton.cancel;
    $("[name=start]").click(function () {
        var lineid = $("[name=lineid]").attr("truevalue");
        $.ajax({
            type: "POST",
            url: 'xml',
            data: 'method=gw.command.set&action=capture&line_id=' + lineid + '&tmp=' + Math.random(),
            dataType: 'xml',
            error: function (xml) {
                $(xml).find('Error').each(function () {
                    var errortext = $(this).attr('msg');
                    //show_message("err",errortext);
                    alert(errortext);
                });
            },
            success: function (xml) {
                var stat = $(xml).find('rsp').attr("stat");
                if (stat == "ok" || stat == "reboot" || stat == "restart") {
                    //show_message("success",success);
                    alert(success);
                } else if (stat == "fail") {
                    $(xml).find('Error').each(function () {
                        var code = $(this).attr('code');
                        if (code == 4) {
                            //show_message("err",cannotModify);
                            alert(cannotModify);
                        } else if (code == 5) {
                            gotologin();
                        } else if (code == 22) {
                            //show_message("err",noComp);
                            alert(noComp);
                        } else if (code == 24) {
                            //show_message("err",already);
                            alert(already);
                        }
                    });
                } else if (stat == "info") {
                    var msg = $(xml).find('Capture').attr("msg");
                    if (msg == "input line_id to start") {
                        // show_message("err",input);
                        alert(input);
                    }
                }
            }
        });
    });
    $("[name=stop]").click(function () {
        $.ajax({
            type: "POST",
            url: 'xml',
            data: 'method=gw.command.set&action=stop_capture&tmp=' + Math.random(),
            dataType: 'xml',
            error: function (xml) {
                $(xml).find('Error').each(function () {
                    var errortext = $(this).attr('msg');
                    // show_message("err",errortext);
                    alert(errortext);
                });
            },
            success: function (xml) {
                var stat = $(xml).find('rsp').attr("stat");
                if (stat == "ok" || stat == "reboot" || stat == "restart") {
                    $.ajax({
                        type: "GET",
                        url: "../fxo.pcm",
                        dataType: "text",
                        timeout: 10000,
                        success: function (data, textStatus) {
                            window.location.href = "../fxo.pcm";
                            getlog1();
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            if (textStatus == "timeout") {
                                // show_message("err",timeout);
                                alert(timeout);
                            } else if (textStatus == "parsererror") {
                                window.location.href = "../fxo.pcm";
                                getlog1();
                            } else {
                                // show_message("err",nofile);
                                alert(nofile);
                            }
                        }
                    });
                } else if (stat == "fail") {
                    $(xml).find('Error').each(function () {
                        var code = $(this).attr('code');
                        if (code == 4) {
                            // show_message("err",cannotModify);
                            alert(cannotModify);
                        } else if (code == 5) {
                            gotologin();
                        } else {
                            // show_message("err",dlfail);
                            alert(dlfail);
                        }
                    });
                } else if (stat == "info") {
                    var msg = $(xml).find('Capture').attr("msg");
                    if (msg == "input line_id to start") {
                        //show_message("err",push);
                        alert(push);
                    }
                }
            }
        });
    });
    $("#button1").click(function () {
        var loopvalue = new Array();
        $.get( "/xml?method=gw.config.get&id=4000&id=4001&tmp=" + Math.random(), function (data) {
            if ($(data).find("rsp").attr("stat") == "ok") {
                var linehtml = '';
                var j = 0;
                var flag = '';
                var flagfxs = '';
                $(data).find('value').each(function () {
                    loopvalue[j] = $(this).text();
                    j++;
                });
                for (var x = 0; x < loopvalue.length; x += 4) {
                    if (x >= loopvalue.length) {
                        linehtml += '<tr><td>&nbsp;</td><td>&nbsp;</td>';
                    } else {
                        linehtml += '<tr><td><input type=checkbox name="checkline" id=fxs' + loopvalue[x].split("/")[2] + ' trueid="' + loopvalue[x].split("/")[1] + '">&nbsp;' + loopvalue[x].split("/")[0] + '</td><td>' + loopvalue[x].split("/")[2] + '</td>';
                    }

                    if ((x + 1) >= loopvalue.length) {
                        linehtml += '<td>&nbsp;</td><td>&nbsp;</td>';
                    } else {
                        linehtml += '<td><input type=checkbox name="checkline" id=fxs' + loopvalue[x + 1].split("/")[2] + ' trueid="' + loopvalue[x + 1].split("/")[1] + '">&nbsp;' + loopvalue[x + 1].split("/")[0] + '</td><td>' + loopvalue[x + 1].split("/")[2] + '</td>';
                    }

                    if ((x + 2) >= loopvalue.length) {
                        linehtml += '<td>&nbsp;</td><td>&nbsp;</td>';
                    } else {
                        linehtml += '<td><input type=checkbox name="checkline" id=fxs' + loopvalue[x + 2].split("/")[2] + ' trueid="' + loopvalue[x + 2].split("/")[1] + '">&nbsp;' + loopvalue[x + 2].split("/")[0] + '</td><td>' + loopvalue[x + 2].split("/")[2] + '</td>';
                    }

                    if ((x + 3) >= loopvalue.length) {
                        linehtml += '<td>&nbsp;</td><td>&nbsp;</td></tr>';
                    } else {
                        linehtml += '<td><input type=checkbox name="checkline" id=fxs' + loopvalue[x + 3].split("/")[2] + ' trueid="' + loopvalue[x + 3].split("/")[1] + '">&nbsp;' + loopvalue[x + 3].split("/")[0] + '</td><td>' + loopvalue[x + 3].split("/")[2] + '</td></tr>';
                    }
                }
                linehtml += '<tr><td colspan=8><input type=button name="ok" id=ok value="' + ensure + '" class="button"><input type=button name="reset" value="' + cancel + '"  class="button"></td></tr>';
                $("#linetest").empty();
                $("#linetest").html(linehtml);
                $("[name=checkline]").click(function () {
                    var ID = $(this).attr('trueid');
                    $(":checkbox").each(function () {
                        if ($(this).attr('trueid') != ID) {
                            $(this).attr("checked", "");
                        }
                    });
                });
                $("#ok").click(function () {
                    var lines = '';
                    var ids = '';
                    $(":checkbox").each(function () {
                        if ($(this).attr("id").indexOf("fxs") >= 0 && $(this).attr("checked")) {
                            lines = $(this).attr("id").split("fxs")[1];
                            ids = $(this).attr("trueid");
                            $("[name=lineid]").attr("value", lines);
                            $("[name=lineid]").attr("truevalue", ids);
                            check();
                        }
                    });
                    if (lines == "") {
                        $("[name=lineid]").attr("value", "");
                    }
                    $("#layer").hide();
                });
                $("[name=reset]").click(function () {
                    $("td").each(function () {});
                    $("#layer").hide();
                });
                var linevalue = $("[name=lineid]").attr("value");
                $("#fxs" + linevalue).attr("checked", true);
            } else {
                $(data).find('Error').each(function () {
                    var code = $(this).attr('code');
                    if (code == 4) {
                        // show_message("err",cannotModify);
                        alert(cannotModify);
                    } else if (code == 5) {
                        gotologin();
                    } else {
                        var strArray = $(this).attr('msg').split(" ");
                        var mid = strArray[1].substr(2);
                        if (mid == 401) {
                            // show_message("err",lineNumberError);
                            alert(lineNumberError);
                        } else {
                            showerror(mid);
                        }
                    }
                });
            }
        });
        $("#layer").show();
    });



function getlog1() {
    $.ajax({
        type: "GET",
        url: "../fxo1.pcm",
        dataType: "text",
        timeout: 10000,
        success: function (data, textStatus) {
            window.location.href = "../fxo1.pcm";
            getlog2();
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            if (textStatus == "timeout") {} else if (textStatus == "parsererror") {
                window.location.href = "../fxo1.pcm";
                getlog2();
            }
        }
    });
}

function getlog2() {
    $.ajax({
        type: "GET",
        url: "../fxo2.pcm",
        dataType: "text",
        timeout: 10000,
        success: function (data, textStatus) {
            window.location.href = "../fxo2.pcm";
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            if (textStatus == "timeout") {} else if (textStatus == "parsererror") {
                window.location.href = "../fxo2.pcm";
            }
        }
    });
}

function check() {
    var lineid = $("[name=lineid]").val();
    $.ajax({
        type: "POST",
        url: 'xml',
        data: 'method=gw.config.get&id=4000&id=4001&tmp=' + Math.random(),
        dataType: "xml",
        error: function (xml) {
            $(xml).find('Error').each(function () {
                var errortext = $(this).attr('msg');
               // show_message("err",errortext);
               alert(errortext);
            });
        },
        success: function (xml) {
            var stat = $(xml).find('rsp').attr("stat");
            if (stat == "ok" || stat == "reboot" || stat == "restart") {
                var linename = "";
                var linenumber = "";
                var IDS = "";
                var flag = "";
                $(xml).find("value").each(function () {
                    if ($(this).text().split("/")[2] == lineid) {
                        linenumber = $(this).text().split("/")[2];
                        linename = $(this).text().split("/")[0];
                        IDS = $(this).text().split("/")[1];
                        flag = "true";
                    }
                });
                if (flag == '') {
                    $("#note").html('<font color="red">' + noline + '</font>');
                    $("[name=lineid]").attr("truevalue", "");
                } else if (flag == "true") {
                    $("#note").html(name + linename +"&nbsp;&nbsp;"+ num + linenumber);
                    $("[name=lineid]").attr("truevalue", IDS);
                }
            } else if (stat == "fail") {
                $(xml).find('Error').each(function () {
                    var code = $(this).attr('code');
                    if (code == 4) 
                    alert(cannotModify);
                    //show_message("err",cannotModify);
                    else if (code == 5) gotologin();
                });
            }
        }
    });
}

</script>

